//
//  SJXQViewController.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SJXQViewController.h"

@interface SJXQViewController ()<JSDropDownMenuDataSource, JSDropDownMenuDelegate, UICollectionViewDataSource, UICollectionViewDelegate>{
    // 数据
    NSMutableArray *_data1;
    NSMutableArray *_data2;
    NSMutableArray *_data3;
    // 当前下标
    NSInteger _currentData1Index;
    NSInteger _currentData2Index;
    NSInteger _currentData3Index;
    JSDropDownMenu *menu;
}
// 保存数据的数组
@property (nonatomic, strong) NSArray *dataArr;
// 保存页面数据的数组
@property (nonatomic, strong) NSMutableArray *yemianArr;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) NSInteger cate;
@property (nonatomic, assign) NSInteger sort;
@property (nonatomic, strong) NSString *subcate;
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, assign) NSInteger page;
@end

@implementation SJXQViewController
//- (void)viewWillAppear:(BOOL)animated
//{
//    self.navigationController.navigationBarHidden = YES;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.page = 2;
    self.i = 0;
    [self dataHandler];
    [self createmenu];
    // 定义collectionView的layout
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2 / 0.618);
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 110, WIDTH, HEIGHT - 110) collectionViewLayout:_layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    // 注册collectionView的cell重用池
    [_collectionView registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(changeURL)];
    [_collectionView.mj_header beginRefreshing];
    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [_collectionView.mj_footer beginRefreshing];
    [self.view addSubview:_collectionView];
}
- (void)loadNewData
{
    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://course.wx.jaeapp.com/index.php?c=market&a=products&cate=%ld&page=%ld&pagesize=20&sort=%ld&subcate=%@&vid=15", self.cate, self.page, self.sort, self.subcate] completion:^(id result) {
        NSArray *arrTemp = [FirstModel modelHandlerWithArray:result[@"data"]];
        for (FirstModel *m in arrTemp) {
            [_yemianArr addObject:m];
        }
        [_collectionView reloadData];
        [_collectionView.mj_header endRefreshing];
    }];
    self.page++;
    [_collectionView.mj_footer endRefreshing];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _yemianArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    FirstModel *m = _yemianArr[indexPath.item];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.picurl]];
    cell.titleLabel.text = m.title;
    cell.priceLabel.text = [NSString stringWithFormat:@"¥%@", m.yh_price];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstModel *m = _yemianArr[indexPath.item];
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.url = [@"http://h5.m.taobao.com/cm/snap/index.html?u_channel=1-23259936&umpChannel=1-23259936&exParams=%7B%22umpChannel%22:%221-23259936%22,%22u_channel%22:%221-23259936%22,%22referer%22:%22showTaoKeItemDetail%22%7D&ttid=2014_0_23259936@baichuan_ios_1.8.3&id=" stringByAppendingString:m.num_iid];
    [self.navigationController pushViewController:webVC animated:YES];
}
// 创建菜单
- (void)createmenu
{
    self.view.backgroundColor = [UIColor whiteColor];
    // 指定默认选中
    _currentData1Index = 0;
    _data1 = [NSMutableArray arrayWithObjects:@"全部", @"饰品", @"包包", @"鞋靴", @"服装配饰", @"家居日常", @"美妆护肤", nil];
    _data2 = [NSMutableArray arrayWithObjects:@"全部", nil];
    _data3 = [NSMutableArray arrayWithObjects:@"新品推荐", @"销量", @"人气", @"好评", @"价格从低到高", @"价格从高到低", nil];
    menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 20) andHeight:45];
    menu.frame = CGRectMake(0, 64, WIDTH, 45);
    menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
    menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
    menu.dataSource = self;
    menu.delegate = self;
    [self.view addSubview:menu];
}
// 个数
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    return 3;
}
// 是否用collection显示
-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{
    return NO;
}
// 右边栏
-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    return NO;
}
// 左边栏比例
-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    return 1;
}

-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    
    if (column == 0) {
        return _currentData1Index;
    }
    if (column == 1) {
        return _currentData2Index;
    }
    if (column == 2) {
        return _currentData3Index;
    }
    return 0;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    
    if (column == 0) {
        return _data1.count;
    } else if (column == 1){
        return _data2.count;
        
    } else if (column == 2){
        return _data3.count;
    }
    return 0;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    switch (column) {
        case 0: return _data1[_currentData1Index];
            break;
        case 1: return _data2[_currentData2Index];
            break;
        case 2: return _data3[_currentData3Index];
            break;
        default:
            return nil;
            break;
    }
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    if (indexPath.column == 0) {
        return _data1[indexPath.row];
    } else if (indexPath.column == 1) {
        return _data2[indexPath.row];
    } else {
        return _data3[indexPath.row];
    }
}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    if (indexPath.column == 0) {
        _data2 = [NSMutableArray arrayWithObjects:@"全部", nil];
        _currentData1Index = indexPath.row;
        if (indexPath.row != 0) {
            JishiModel *m = _dataArr[indexPath.row - 1];
            NSArray *arr = m.sonArr;
            for (SonModel *model in arr) {
                [_data2 addObject:model.cate_name];
            }
        }
        if (indexPath.row == 0) {
            self.cate = 0;
        } else {
            self.cate = [[_dataArr[indexPath.row - 1] cate_id] integerValue];
        }
        _currentData2Index = 0;
        [self changeURL];
    } else if(indexPath.column == 1){
        _currentData2Index = indexPath.row;
        if (indexPath.row == 0) {
            self.subcate = @"%28null%29";
        } else {
            self.subcate = [[_dataArr[_currentData1Index - 1] sonArr][indexPath.row - 1] baichuan_id];
        }
        [self changeURL];
    } else{
        _currentData3Index = indexPath.row;
        self.sort = indexPath.row;
        [self changeURL];
    }
}
- (void)changeURL
{
    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://course.wx.jaeapp.com/index.php?c=market&a=products&cate=%ld&page=1&pagesize=20&sort=%ld&subcate=%@&vid=15", self.cate, self.sort, self.subcate] completion:^(id result) {
        self.yemianArr = [FirstModel modelHandlerWithArray:result[@"data"]];
        [_collectionView reloadData];
        [_collectionView.mj_header endRefreshing];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dataHandler
{
    [AFNModel GETWithURL:@"http://m.shougongke.com/index.php?c=Shiji&a=getCateList&vid=15" completion:^(id result) {
        self.dataArr = [JishiModel modelHandlerWithArray:result[@"data"]];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
